package com.kevin.Code.Tree;

/**
 * @author Vinlee Xiao
 * @Classname SubTreeDemo
 * @Description 剑指Offer 26 树的子结构 三刷 CVTE面试
 * @Date 2022/3/14 19:43
 * @Version 1.0
 */
public class SubTreeDemo {

    public static void main(String[] args) {

    }

    /**
     * @param A
     * @param B
     * @return
     */
    public boolean isSubStructure(TreeNode A, TreeNode B) {

        return A != null && B != null && (dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }

    /**
     * 三刷还是不会
     *
     * @param A
     * @param B
     * @return
     */
    public boolean dfs(TreeNode A, TreeNode B) {

        //如果两个节点入围空，则返回true 或者A不为空
        if (B == null) {
            return true;
        }

        if (A != null || A.val != B.val) {
            return false;
        }

        return dfs(A.left, B.left) && dfs(A.right, B.right);
    }
}
